<template>
  <!-- 菜单管理 -->
  <!-- 梁博文 2023年6月5日14点25分 -->
  <div id="sys_menu">
    <el-card style="margin-bottom: 10px">
      <el-button
        @click="saveMenuDialogFlag = true"
        class="custom-button"
        icon="el-icon-plus"
        >添加菜单</el-button
      >
    </el-card>
    <el-card>
      <el-table
        :data="menuList"
        height="600"
        row-key="id"
        border
        stripe
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="menuName" label="菜单名称">
          <template slot-scope="scope">
            {{ scope.row.title }}
          </template>
        </el-table-column>
        <el-table-column prop="url" label="菜单地址"> </el-table-column>
        <el-table-column prop="name" label="name"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column prop="updateTime" label="更新时间"> </el-table-column>
        <el-table-column min-width="100%" fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              class="pass_button"
              @click="openViewMenu(scope.row)"
              type="small"
              icon="el-icon-info"
              >详情
            </el-button>
            <el-button
              class="pass_button"
              @click="openUpdateMenu(scope.row)"
              type="small"
              icon="el-icon-edit"
              >修改
            </el-button>
            <el-button
              class="pass_button"
              @click="openDeleteMenu(scope.row.id)"
              type="small"
              icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 菜单数据添加弹窗 -->
    <el-dialog title="添加菜单" :visible.sync="saveMenuDialogFlag">
      <el-form
        :model="menuForm"
        :rules="menuRules"
        ref="menuForm"
        label-width="180px"
        class="demo-menuForm"
      >
        <el-form-item label="父级菜单" prop="pid">
          <el-input
            v-model="menuForm.ptitle"
            :disabled="true"
            style="width: 80%"
            show-word-limit
            clearable
            placeholder="请选择"
          ></el-input>
          <el-button
            type="primary"
            @click="parentTreeSaveMenuFlag = true"
            plain
            style="background: #d7000f; border-color: #d7000f; color: white"
            >上级</el-button
          >
        </el-form-item>
        <el-form-item label="菜单名称" prop="title">
          <el-input v-model="menuForm.title"></el-input>
        </el-form-item>
        <el-form-item label="菜单路由name" prop="name">
          <el-input v-model="menuForm.name"></el-input>
        </el-form-item>
        <el-form-item label="路由跳转path" prop="url">
          <el-input v-model="menuForm.url"></el-input>
        </el-form-item>
        <el-form-item label="菜单views下的文件路径" prop="curl">
          <el-input v-model="menuForm.curl"></el-input>
        </el-form-item>
        <el-form-item label="菜单名称" prop="title">
          <el-input v-model="menuForm.title"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitSaveMenuForm('menuForm')"
            >立即添加</el-button
          >
          <el-button @click="resetSaveMenuForm('menuForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 菜单数据修改弹窗 -->
    <el-dialog title="修改菜单" :visible.sync="updateMenuDialogFlag">
      <el-form
        :model="menuUpdateForm"
        :rules="menuRules"
        ref="menuUpdateForm"
        label-width="180px"
        class="demo-menuForm"
      >
        <el-form-item label="父级菜单" prop="pid">
          <el-input
            v-model="menuUpdateForm.ptitle"
            :disabled="true"
            style="width: 80%"
            show-word-limit
            clearable
          ></el-input>
          <el-button
            type="primary"
            @click="parentTreeUpdateMenuFlag = true"
            plain
            style="background: #d7000f; border-color: #d7000f; color: white"
            >上级</el-button
          >
        </el-form-item>
        <el-form-item label="菜单名称" prop="title">
          <el-input v-model="menuUpdateForm.title"></el-input>
        </el-form-item>
        <el-form-item label="菜单路由name" prop="name">
          <el-input v-model="menuUpdateForm.name"></el-input>
        </el-form-item>
        <el-form-item label="路由跳转path" prop="url">
          <el-input v-model="menuUpdateForm.url"></el-input>
        </el-form-item>
        <el-form-item label="菜单views下的文件路径" prop="curl">
          <el-input v-model="menuUpdateForm.curl"></el-input>
        </el-form-item>
        <el-form-item label="菜单名称" prop="title">
          <el-input v-model="menuUpdateForm.title"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitUpdateMenuForm('menuUpdateForm')"
            >立即修改</el-button
          >
          <el-button @click="resetUpdateMenuForm('menuUpdateForm')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 菜单数据修改弹窗 -->
    <el-dialog title="查看详情" :visible.sync="viewMenuDialogFlag">
      <el-form
        :model="menuUpdateForm"
        :rules="menuRules"
        ref="menuUpdateForm"
        label-width="180px"
        class="demo-menuForm"
      >
        <el-form-item label="父级菜单" prop="pid">
          <el-input
            v-model="menuUpdateForm.ptitle"
            :disabled="true"
            show-word-limit
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="菜单名称" prop="title">
          <el-input :disabled="true" v-model="menuUpdateForm.title"></el-input>
        </el-form-item>
        <el-form-item label="菜单路由name" prop="name">
          <el-input :disabled="true" v-model="menuUpdateForm.name"></el-input>
        </el-form-item>
        <el-form-item label="路由跳转path" prop="url">
          <el-input :disabled="true" v-model="menuUpdateForm.url"></el-input>
        </el-form-item>
        <el-form-item label="菜单views下的文件路径" prop="curl">
          <el-input :disabled="true" v-model="menuUpdateForm.curl"></el-input>
        </el-form-item>
        <el-form-item label="菜单名称" prop="title">
          <el-input :disabled="true" v-model="menuUpdateForm.title"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="title">
          <el-input
            :disabled="true"
            v-model="menuUpdateForm.createTime"
          ></el-input>
        </el-form-item>
        <el-form-item label="修改时间" prop="title">
          <el-input
            :disabled="true"
            v-model="menuUpdateForm.updateTime"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="viewMenuDialogFlag = false"
            >关闭</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 父级新增窗口选项 -->
    <el-dialog
      title="父级"
      :visible.sync="parentTreeSaveMenuFlag"
      width="30%"
      append-to-body
    >
      <el-tree
        class="filter-tree"
        :data="menuList"
        :props="defaultProps"
        default-expand-all
        :filter-node-method="filterNode"
        @node-click="saveMenuCallTree"
        ref="tree"
      >
      </el-tree>
    </el-dialog>
    <!-- 父级修改窗口选项 -->
    <el-dialog
      title="父级"
      :visible.sync="parentTreeUpdateMenuFlag"
      width="30%"
      append-to-body
    >
      <el-tree
        class="filter-tree"
        :data="menuList"
        :props="defaultProps"
        default-expand-all
        :filter-node-method="filterNode"
        @node-click="updateMenuCallTree"
        ref="tree"
      >
      </el-tree>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    // views下的文件路径 校验规则
    const checkCurl = (rule, value, callback) => {
      // 禁止中文输入正则表达式
      const reg = /^[^\u4e00-\u9fa5\d]+$/;
      if (value === "") {
        callback();
      } else if (!reg.test(value)) {
        return callback(new Error("输入不规范，请重新输入"));
      } else {
        this.checkMenuCurl(value).then((res) => {
          if (res.flag) {
            callback();
          } else {
            callback(new Error("views下的文件路径已重复，请更改"));
          }
        });
      }
    };
    // 路由跳转path 校验规则
    const checkUrl = (rule, value, callback) => {
      // 禁止中文输入正则表达式
      const reg = /^[^\u4e00-\u9fa5\d]+$/;
      if (value === "") {
        callback(new Error("请输入路由跳转path"));
      } else if (!reg.test(value)) {
        callback(new Error("输入不规范，请重新输入"));
      } else {
        if (value === "/") {
          callback();
        } else {
          this.checkMenuUrl(value).then((res) => {
            if (res.flag) {
              callback();
            } else {
            }
          });
        }
      }
    };
    // 路由name 校验规则
    const checkName = (rule, value, callback) => {
      // 纯英文正则表达式
      const reg = /[^\a-\z\A-\Z]/g;
      if (value === "") {
        callback(new Error("请输入路由name"));
      } else if (reg.test(value)) {
        callback(new Error("路由name只能输入纯英文!"));
      } else {
        this.checkMenuName(value).then((res) => {
          if (res.flag) {
            callback();
          } else {
          }
        });
      }
    };
    // 路由描述（路由名称） 校验规则
    const checkTitle = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入路由名称"));
      } else {
        this.checkMenuTitle(value).then((res) => {
          if (res.flag) {
            callback();
          } else {
          }
        });
      }
    };
    return {
      // 菜单列表
      menuList: [],
      // 菜单对象
      menuForm: {
        name: "",
        // 描述
        title: "",
        // url
        url: "",
        // 父级id
        pid: 0,
        // 图标
        icon: "",
        // views下的文件路径
        curl: "",
        // 父级描述
        ptitle: "",
      },
      // 菜单修改对象
      menuUpdateForm: {
        name: "",
        // 描述
        title: "",
        // 路由跳转path
        url: "",
        // 父级id
        pid: "",
        // 图标
        icon: "",
        // views下的文件路径
        curl: "",
        // 父级描述
        ptitle: "",
        // 创建时间
        createTime: "",
        // 修改时间
        updateTime: "",
      },
      // 新增菜单弹窗状态
      saveMenuDialogFlag: false,
      // 修改菜单弹窗状态
      updateMenuDialogFlag: false,
      // 查看详情菜单弹窗状态
      viewMenuDialogFlag: false,
      // 父级窗口选项弹框状态
      parentTreeSaveMenuFlag: false,
      // 父级窗口选项弹框状态
      parentTreeUpdateMenuFlag: false,
      // 添加菜单表单规则
      menuRules: {
        // 路由描述（路由名称）
        title: [
          { validator: checkTitle, trigger: 'blur' }
        ],
        // 路由name
        name: [
          { validator: checkName, trigger: 'blur' }
        ],
        // 路由跳转path
        url: [
          { validator: checkUrl, trigger: 'blur' }
        ],
        // views下的文件路径
        curl: [
          { validator: checkCurl, trigger: 'blur' }
        ]
      },
      // 父级树菜单默认列表规则
      defaultProps: {
        children: "child",
        label: "title",
      },
    };
  },
  mounted() {
    // 初始化获取菜单列表
    this.getMenuList();
  },
  methods: {
    // 菜单描述查重
    checkMenuTitle(title) {
      return new Promise((resolve, reject) => {
        this.$axios
          .get("/sysMenu/checkMenuTitle?title=" + title)
          .then((res) => {
            resolve(res.data);
          });
      });
    },
    // 菜单name查重
    checkMenuName(name) {
      return new Promise((resolve, reject) => {
        this.$axios.get("/sysMenu/checkMenuName?name=" + name).then((res) => {
          resolve(res.data);
        });
      });
    },
    // 菜单路径查重
    checkMenuUrl(url) {
      return new Promise((resolve, reject) => {
        this.$axios.get("/sysMenu/checkMenuUrl?url=" + url).then((res) => {
          resolve(res.data);
        });
      });
    },
    // 菜单views下的文件路径
    checkMenuCurl(curl) {
      return new Promise((resolve, reject) => {
        this.$axios
          .get("/sysMenu/checkMenuCurl?curl=" + curl)
          .then((res) => {
            resolve(res.data);
          })
          .catch((error) => {
            this.$message.error("服务器崩溃了，请稍后再尝试使用！");
          });
      });
    },
    // 获取菜单信息列表
    getMenuList() {
      this.$axios
        .get("/sysMenu/getMenuByUser")
        .then((res) => {
          this.menuList = res.data.data;
        })
        .catch((error) => {
          this.$message.error("服务器崩溃了，请稍后再尝试使用！");
        });
    },
    // 提交添加菜单表单
    submitSaveMenuForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.saveMenu();
        } else {
          return false;
        }
      });
    },
    // 删除点击提示框
    openDeleteMenu(id) {
      this.$confirm("此操作将永久删除该菜单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteMenuById(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteMenuById(id) {
      this.$axios
        .delete("/sysMenu/deleteMenu?id=" + id)
        .then((res) => {
          if (res.data.flag) {
            this.message("删除成功！", "success");
            // 初始化菜单列表
            this.getMenuList();
          } else {
            this.message("对不起，添加失败！", "warning");
          }
        })
        .catch((error) => {
          this.$message.error("服务器崩溃了，请稍后再尝试使用！");
        });
    },
    // 点击查看按钮
    openViewMenu(form) {
      this.viewMenuDialogFlag = true;
      this.menuUpdateForm.id = form.id;
      this.menuUpdateForm.pid = form.pid;
      this.menuUpdateForm.ptitle = form.ptitle;
      this.menuUpdateForm.title = form.title;
      this.menuUpdateForm.name = form.name;
      this.menuUpdateForm.url = form.url;
      this.menuUpdateForm.curl = form.curl;
      this.menuUpdateForm.createTime = form.createTime;
      this.menuUpdateForm.updateTime = form.updateTime;
    },
    // 点击修改菜单按钮
    openUpdateMenu(form) {
      this.updateMenuDialogFlag = true;
      this.menuUpdateForm.id = form.id;
      this.menuUpdateForm.pid = form.pid;
      this.menuUpdateForm.ptitle = form.ptitle;
      this.menuUpdateForm.title = form.title;
      this.menuUpdateForm.name = form.name;
      this.menuUpdateForm.url = form.url;
      this.menuUpdateForm.curl = form.curl;
    },
    // 提交修改菜单表单
    submitUpdateMenuForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.updateMenu();
        } else {
          return false;
        }
      });
    },
    // 添加菜单
    saveMenu() {
      this.$axios
        .post("/sysMenu/saveMenu", this.menuForm)
        .then((res) => {
          if (res.data.flag) {
            this.message("恭喜你，添加成功！", "success"); 
            this.menuForm = {
              name: "",
              // 描述
              title: "",
              // url
              url: "",
              // 父级id
              pid: 0,
              // 图标
              icon: "",
              // views下的文件路径
              curl: "",
              // 父级描述
              ptitle: "",
            };
            this.saveMenuDialogFlag = false;
            // 初始化菜单列表
            this.getMenuList();
          } else {
            this.message("对不起，添加失败！", "warning");
          }
        })
        .catch((error) => {
          this.$message.error("服务器崩溃了，请稍后再尝试使用！");
        });
    },
    // 修改菜单
    updateMenu() {
      this.$axios
        .put("/sysMenu/updateMenu", this.menuUpdateForm)
        .then((res) => {
          if (res.data.flag) {
            this.message("恭喜你，修改菜单成功！", "success");
            this.menuUpdateForm = {
              name: "",
              // 描述
              title: "",
              // url
              url: "",
              // 父级id
              pid: 0,
              // 图标
              icon: "",
              // views下的文件路径
              curl: "",
              // 父级描述
              ptitle: "",
            };
            this.updateMenuDialogFlag = false;
            // 初始化菜单列表
            this.getMenuList();
          } else {
            this.message("对不起，修改失败！", "warning");
          }
        })
        .catch((error) => {
          this.$message.error("服务器崩溃了，请稍后再尝试使用！");
        });
    },
    // 重置添加菜单表单
    resetSaveMenuForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 重置修改菜单表单
    resetSaveMenuForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 树节点回调
    saveMenuCallTree(data) {
      this.menuForm.pid = data.id;
      this.menuForm.ptitle = data.title;
      this.parentTreeSaveMenuFlag = false;
    },
    // 树节点回调
    updateMenuCallTree(data) {
      this.menuUpdateForm.pid = data.id;
      this.menuUpdateForm.ptitle = data.title;
      this.parentTreeUpdateMenuFlag = false;
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 消息提示框
    message(message, type) {
      this.$message({
        message: message,
        type: type,
      });
    },
  },
};
</script>

<style scoped>
#sys_menu {
  /* color: #03b385; */
}
</style>
